package wifiunion.com.notice.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import wifiunion.com.notice.Constants;
import wifiunion.com.notice.GlideCircleTransform;
import wifiunion.com.notice.R;
import wifiunion.com.notice.entity.NoticeInfoList;
import wifiunion.com.notice.entity.User;
import wifiunion.com.notice.utils.SharedPreferencesUtils;

/**
 * 通知列表适配器
 */

public class NoticeMainAdapter extends RecyclerView.Adapter<NoticeListViewHolder> {

    private Context mContext;

    private List<NoticeInfoList> data = new ArrayList<NoticeInfoList>();


    private View.OnClickListener mItemClick;

    /**
     * 0 listview
     * 1 gridview
     */
    private int mType;

    private User mUser;


    public NoticeMainAdapter(Context context, int type, List<NoticeInfoList> dt, View.OnClickListener itemClick) {
        mContext = context;
        mType = type;
        data = dt;
        mItemClick = itemClick;
        mUser = SharedPreferencesUtils.getUserSharedPreferences(mContext);
    }


    @Override
    public NoticeListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view;
        if (mType == 0) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.notice_list_item, parent, false);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.notice_grid_item, parent, false);
        }

        NoticeListViewHolder cv = new NoticeListViewHolder(view, mType);


        return cv;
    }

    @Override
    public void onBindViewHolder(NoticeListViewHolder holder, int position) {


        NoticeInfoList noticeInfo = data.get(position);
        if (mType == 0) {

            //识别时段
//            if(noticeInfo.getIsdate() == 0)
//            {
//                //判断时间是否为空
//                if(null !=noticeInfo.getStartTime() && null !=noticeInfo.getEndTime())
//                {
//
//                    holder.mNoticelisttimestv.setText(noticeInfo.getStartTime().substring(0,noticeInfo.getStartTime().length() -3)+"~"+noticeInfo.getEndTime().substring(0,noticeInfo.getEndTime().length() -3));
//                }
//            }
//            else
//            {
//
//                holder.mNoticelisttimestv.setText(noticeInfo.getStartDate()+" "+noticeInfo.getEndDate() +"\n"+noticeInfo.getStartTime().substring(0,noticeInfo.getStartTime().length() -3)+"~"+noticeInfo.getEndTime().substring(0,noticeInfo.getEndTime().length() -3));
//            }

            //识别设备
            if (null != noticeInfo.getFromDevice() && noticeInfo.getFromDevice().size() > 0) {
                holder.mNoticelistsbsbtv.setText(noticeInfo.getFromDevice().get(0).getLocationName());
            } else {
                holder.mNoticelistsbsbtv.setText("");
            }

//            holder.mNoticelisttzdxtv.setText("");

            //通知内容
            holder.mNoticelisttznrtv.setText(noticeInfo.getContent());
            if (!TextUtils.isEmpty(noticeInfo.getGroupName())) {
                holder.mNoticelistGrouptv.setText(noticeInfo.getGroupName());
            } else {
                holder.mNoticelistGrouptv.setText("");
            }

            if (null != noticeInfo.getFromUser() && noticeInfo.getFromUser().size() > 0) {
                if (noticeInfo.getFromUser().size() == 1) {
                    if (!"0".equals(noticeInfo.getFromUser().get(0).getUuid())) {
                        Glide.with(mContext).load(Constants.HTTP_URL_IMAGESERVER + noticeInfo.getFromUser().get(0).getComparisonPic()).placeholder(R.drawable.default_header)
                                .error(R.drawable.default_header).centerCrop().into(holder.mNoticelistsbdx1iv);
                    } else {
//                        Glide.with(mContext).load(Constants.HTTP_URL_IMAGESERVER + noticeInfo.getFromUser().get(0).getComparisonPic()).placeholder(R.drawable.msr)
//                                .error(R.drawable.msr).fitCenter().into(holder.mNoticelistsbdx1iv);

                        Glide.with(mContext)
                                .load(R.drawable.msr)
                                .placeholder(R.drawable.msr).error(R.drawable.msr).centerCrop().into(holder.mNoticelistsbdx1iv);

                    }

                    Glide.with(mContext).load(Constants.HTTP_URL_IMAGESERVER).placeholder(R.drawable.default_header)
                            .error(R.drawable.default_header).centerCrop().into(holder.mNoticelistsbdx2iv);
                } else if (noticeInfo.getFromUser().size() >= 2) {

                    if (!"0".equals(noticeInfo.getFromUser().get(0).getUuid())) {
                        Glide.with(mContext).load(Constants.HTTP_URL_IMAGESERVER + noticeInfo.getFromUser().get(0).getComparisonPic()).placeholder(R.drawable.default_header)
                                .error(R.drawable.default_header).centerCrop().into(holder.mNoticelistsbdx1iv);
                    } else {
                        Glide.with(mContext)
                                .load(R.drawable.msr)
                                .placeholder(R.drawable.msr).error(R.drawable.msr).centerCrop().into(holder.mNoticelistsbdx1iv);
                    }

                    if (!"0".equals(noticeInfo.getFromUser().get(1).getUuid())) {
                        Glide.with(mContext).load(Constants.HTTP_URL_IMAGESERVER + noticeInfo.getFromUser().get(1).getComparisonPic()).placeholder(R.drawable.default_header)
                                .error(R.drawable.default_header).centerCrop().into(holder.mNoticelistsbdx2iv);
                    } else {
                        Glide.with(mContext)
                                .load(R.drawable.msr)
                                .placeholder(R.drawable.msr).error(R.drawable.msr).centerCrop().into(holder.mNoticelistsbdx2iv);
                    }


                }

            } else {
                Glide.with(mContext).load(Constants.HTTP_URL_IMAGESERVER).placeholder(R.drawable.default_header)
                        .error(R.drawable.default_header).centerCrop().into(holder.mNoticelistsbdx1iv);
                Glide.with(mContext).load(Constants.HTTP_URL_IMAGESERVER).placeholder(R.drawable.default_header)
                        .error(R.drawable.default_header).centerCrop().into(holder.mNoticelistsbdx2iv);
            }

            if (noticeInfo.getState() == 2) {
                holder.mNoticelistOverdue.setVisibility(View.VISIBLE);
            } else {
                holder.mNoticelistOverdue.setVisibility(View.GONE);
            }

            holder.mNoticelistdeletetv.setTag(position);
            holder.mNoticelistedittv.setTag(position);
            holder.mNoticelistlooktv.setTag(position);
            holder.mNoticelistRecordtv.setTag(position);
            holder.mNoticelistcopytv.setTag(position);


            if(noticeInfo.getIsHaveRel() == 0)
            {
//                Drawable drawable = mContext.getDrawable(R.drawable.device_guanlian);
                Drawable drawable =  ContextCompat.getDrawable(mContext, R.drawable.device_guanlian);

                holder.mNoticelistRelevance.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
                holder.mNoticelistRelevance.setCompoundDrawablePadding(mContext.getResources().getDimensionPixelOffset(R.dimen.dp_8));
            }else
            {
//                Drawable drawable = mContext.getDrawable(R.drawable.device_yiguanlian);
                Drawable drawable =  ContextCompat.getDrawable(mContext, R.drawable.device_yiguanlian);
                holder.mNoticelistRelevance.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
                holder.mNoticelistRelevance.setCompoundDrawablePadding(mContext.getResources().getDimensionPixelOffset(R.dimen.dp_8));
            }
            holder.mNoticelistRelevance.setTag(position);

            holder.mNoticelistlooktv.setOnClickListener(mItemClick);
            holder.mNoticelistdeletetv.setOnClickListener(mItemClick);
            holder.mNoticelistedittv.setOnClickListener(mItemClick);
            holder.mNoticelistRecordtv.setOnClickListener(mItemClick);
            holder.mNoticelistcopytv.setOnClickListener(mItemClick);
            holder.mNoticelistRelevance.setOnClickListener(mItemClick);
        } else {
            //网格展示
            if (null != noticeInfo.getFromUser() && noticeInfo.getFromUser().size() > 0) {
                if (noticeInfo.getFromUser().size() == 1) {

                    holder.singleHeadLL.setVisibility(View.VISIBLE);
                    holder.doubleHeadLL.setVisibility(View.GONE);
                    holder.threeHeadLL.setVisibility(View.GONE);

                    if (!"0".equals(noticeInfo.getFromUser().get(0).getUuid())) {
                        Glide.with(mContext)
                                .load(Constants.HTTP_URL_IMAGESERVER + noticeInfo.getFromUser().get(0).getComparisonPic())
                                .error(R.drawable.default_header).centerCrop()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegridSingleheaderiv);
                    } else {
                        // 陌生人
                        Glide.with(mContext)
                                .load(R.drawable.msr)
                                .error(R.drawable.msr).fitCenter()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegridSingleheaderiv);

//                        holder.mNoticegridSingleheaderiv.setBackgroundResource(R.drawable.msr);

                    }


                    holder.mNoticegridsbdxtv.setText(noticeInfo.getFromUser().get(0).getName() == null ? "未登记人员" : noticeInfo.getFromUser().get(0).getName());

                } else if (noticeInfo.getFromUser().size() == 2) {

                    holder.singleHeadLL.setVisibility(View.GONE);
                    holder.doubleHeadLL.setVisibility(View.VISIBLE);
                    holder.threeHeadLL.setVisibility(View.GONE);

                    if (!"0".equals(noticeInfo.getFromUser().get(0).getUuid())) {
                        Glide.with(mContext)
                                .load(Constants.HTTP_URL_IMAGESERVER + noticeInfo.getFromUser().get(0).getComparisonPic())
                                .error(R.drawable.default_header).centerCrop()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriDoubleheader1iv);
                    } else {
                        Glide.with(mContext)
                                .load(R.drawable.msr)
                                .error(R.drawable.msr).fitCenter()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriDoubleheader1iv);
                    }
                    if (!"0".equals(noticeInfo.getFromUser().get(1).getUuid())) {
                        Glide.with(mContext)
                                .load(Constants.HTTP_URL_IMAGESERVER + noticeInfo.getFromUser().get(1).getComparisonPic())
                                .error(R.drawable.default_header).centerCrop()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriDoubleheader2iv);
                    } else {
                        Glide.with(mContext)
                                .load(R.drawable.msr)
                                .error(R.drawable.msr).fitCenter()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriDoubleheader2iv);
                    }
                    holder.mNoticegridsbdxtv.setText((noticeInfo.getFromUser().get(0).getName() == null ? "未登记人员" : noticeInfo.getFromUser().get(0).getName()) + ";" + (noticeInfo.getFromUser().get(1).getName() == null ? "未登记人员" : noticeInfo.getFromUser().get(1).getName()));

                } else if (noticeInfo.getFromUser().size() > 2) {

                    holder.singleHeadLL.setVisibility(View.GONE);
                    holder.doubleHeadLL.setVisibility(View.GONE);
                    holder.threeHeadLL.setVisibility(View.VISIBLE);

                    if (!"0".equals(noticeInfo.getFromUser().get(0).getUuid())) {
                        Glide.with(mContext)
                                .load(Constants.HTTP_URL_IMAGESERVER + noticeInfo.getFromUser().get(0).getComparisonPic())
                                .error(R.drawable.default_header).centerCrop()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriThreeheader1iv);
                    } else {
                        Glide.with(mContext)
                                .load(R.drawable.msr)
                                .error(R.drawable.msr).fitCenter()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriThreeheader1iv);

                    }

                    if (!"0".equals(noticeInfo.getFromUser().get(1).getUuid())) {
                        Glide.with(mContext)
                                .load(Constants.HTTP_URL_IMAGESERVER + noticeInfo.getFromUser().get(1).getComparisonPic())
                                .error(R.drawable.default_header).centerCrop()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriThreeheader2iv);
                    } else {
                        Glide.with(mContext)
                                .load(R.drawable.msr)
                                .error(R.drawable.msr).fitCenter()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriThreeheader2iv);
                    }

                    if (!"0".equals(noticeInfo.getFromUser().get(2).getUuid())) {
                        Glide.with(mContext)
                                .load(Constants.HTTP_URL_IMAGESERVER + noticeInfo.getFromUser().get(2).getComparisonPic())
                                .error(R.drawable.default_header).centerCrop()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriThreeheader3iv);

                    } else {
                        Glide.with(mContext)
                                .load(R.drawable.msr)
                                .error(R.drawable.msr).fitCenter()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriThreeheader3iv);
                    }


                    holder.mNoticegridsbdxtv.setText((noticeInfo.getFromUser().get(0).getName() == null ? "未登记人员" : noticeInfo.getFromUser().get(0).getName()) + ";" + (noticeInfo.getFromUser().get(1).getName() == null ? "未登记人员" : noticeInfo.getFromUser().get(1).getName()) + ";" + (noticeInfo.getFromUser().get(2).getName() == null ? "未登记人员" : noticeInfo.getFromUser().get(2).getName()));


                }

            } else {
                holder.singleHeadLL.setVisibility(View.GONE);
                holder.doubleHeadLL.setVisibility(View.GONE);
                holder.threeHeadLL.setVisibility(View.GONE);
                holder.mNoticegridsbdxtv.setText("");
//                holder.mNoticegridsbdxtv.setText("");

            }

//            if (null != noticeInfo.getStartTime() && null != noticeInfo.getEndTime()) {
//                holder.mNoticegridtimestv.setText(noticeInfo.getStartTime().substring(0, noticeInfo.getStartTime().length() - 3) + "~" + noticeInfo.getEndTime().substring(0, noticeInfo.getEndTime().length() - 3));
//            } else {
//                holder.mNoticegridtimestv.setText("");
//            }

//            if(noticeInfo.getIsdate() == 1)
//            {
//                holder.mNoticegridtimestv.setText(noticeInfo.getStartDate() +" " + noticeInfo.getStartTime().substring(0, noticeInfo.getStartTime().length() - 3) + " ~ "+"\n" +noticeInfo.getEndDate() +" " +noticeInfo.getEndTime().substring(0, noticeInfo.getEndTime().length() - 3));
//            }
//            else
//            {
//                holder.mNoticegridtimestv.setText("不限");
//            }


            if (!TextUtils.isEmpty(noticeInfo.getCycle())) {
                StringBuffer sb = new StringBuffer();
                ArrayList<String> list = new ArrayList<>();
                if (noticeInfo.getCycle().contains("1")) {
                    list.add("一");
                }
                if (noticeInfo.getCycle().contains("2")) {
                    list.add("二");
                }
                if (noticeInfo.getCycle().contains("3")) {
                    list.add("三");
                }
                if (noticeInfo.getCycle().contains("4")) {
                    list.add("四");
                }
                if (noticeInfo.getCycle().contains("5")) {
                    list.add("五");
                }
                if (noticeInfo.getCycle().contains("6")) {
                    list.add("六");
                }
                if (noticeInfo.getCycle().contains("7")) {
                    list.add("日");
                }

                if (null != list && list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        sb.append(list.get(i));
                        if (i < list.size() - 1) {
                            sb.append(",");
                        }
                    }

                }
                holder.mNoticegridweektv.setText(sb.toString());

            } else {
                holder.mNoticegridweektv.setText("");
            }


            if (noticeInfo.getIsdate() == 1) {
                holder.mNoticegriddatetv.setText(noticeInfo.getStartDate() + " ~ " + noticeInfo.getEndDate());
            } else {
                holder.mNoticegriddatetv.setText("不限");
            }


            if (!TextUtils.isEmpty(noticeInfo.getStartTime()) && !TextUtils.isEmpty(noticeInfo.getEndTime())) {
                holder.mNoticegridtimetv.setText(noticeInfo.getStartTime().substring(0, noticeInfo.getStartTime().length() - 3) + " ~ " + noticeInfo.getEndTime().substring(0, noticeInfo.getStartTime().length() - 3));
            } else {
                holder.mNoticegridtimetv.setText("");
            }


//            if (null != noticeInfo.getFromDevice() && noticeInfo.getFromDevice().size() > 0) {
//                holder.mNoticegridsbsbtv.setText(noticeInfo.getFromDevice().get(0).getLocationName());
//            } else {
//                holder.mNoticegridsbsbtv.setText("");
//            }


            if (null != noticeInfo.getFromDevice() && noticeInfo.getFromDevice().size() > 0) {
                switch (noticeInfo.getFromDevice().size()) {
                    case 1:
                        holder.mNoticegridsbsbtv.setText(noticeInfo.getFromDevice().get(0).getLocationName());
                        break;
                    case 2:
                        holder.mNoticegridsbsbtv.setText(noticeInfo.getFromDevice().get(0).getLocationName() + " " + noticeInfo.getFromDevice().get(1).getLocationName());
                        break;
                    case 3:
                        holder.mNoticegridsbsbtv.setText(noticeInfo.getFromDevice().get(0).getLocationName() + " " + noticeInfo.getFromDevice().get(1).getLocationName() + " " + noticeInfo.getFromDevice().get(2).getLocationName());
                        break;
                }

            } else {
                holder.mNoticegridsbsbtv.setText("");
            }


            //通知内容
            holder.mNoticegridtznrtv.setText(noticeInfo.getContent());
            //通知分组

//            if (!TextUtils.isEmpty(noticeInfo.getGroupName())) {
//                holder.mNoticegridGrouptv.setText(noticeInfo.getGroupName());
//            } else {
//                holder.mNoticegridGrouptv.setText("不限");
//            }

            if (noticeInfo.getState() == 2) {
                holder.mNoticeOverdueIv.setVisibility(View.VISIBLE);
            } else {
                holder.mNoticeOverdueIv.setVisibility(View.GONE);
            }

            holder.mNoticegriddeletetv.setTag(position);
            holder.mMerbergridedittv.setTag(position);
            holder.mNoticegridlooktv.setTag(position);
            holder.mNoticegridRecordtv.setTag(position);
            holder.mNoticegridcopytv.setTag(position);


            if(noticeInfo.getIsHaveRel() == 0)
            {
//                Drawable drawable = mContext.getDrawable(R.drawable.device_guanlian);
                Drawable drawable =  ContextCompat.getDrawable(mContext, R.drawable.device_guanlian);


                holder.mNoticegridRelevancetv.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
                holder.mNoticegridRelevancetv.setCompoundDrawablePadding(mContext.getResources().getDimensionPixelOffset(R.dimen.dp_8));
            }else
            {
//                Drawable drawable = mContext.getDrawable(R.drawable.device_yiguanlian);
                Drawable drawable =  ContextCompat.getDrawable(mContext, R.drawable.device_yiguanlian);

                holder.mNoticegridRelevancetv.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
                holder.mNoticegridRelevancetv.setCompoundDrawablePadding(mContext.getResources().getDimensionPixelOffset(R.dimen.dp_8));
            }
            holder.mNoticegridRelevancetv.setTag(position);

            holder.mNoticegridlooktv.setOnClickListener(mItemClick);
            holder.mNoticegriddeletetv.setOnClickListener(mItemClick);
            holder.mMerbergridedittv.setOnClickListener(mItemClick);
            holder.mNoticegridRecordtv.setOnClickListener(mItemClick);
            holder.mNoticegridcopytv.setOnClickListener(mItemClick);
            holder.mNoticegridRelevancetv.setOnClickListener(mItemClick);

//            if (Constants.ISONLINE_VERSION) {
//                holder.mNoticeGridPathrl.setVisibility(View.VISIBLE);
//                holder.mNoticeGridPathrl.setTag(position);
//                holder.mNoticeGridPathrl.setOnClickListener(mItemClick);
//            } else {
                holder.mNoticeGridPathrl.setVisibility(View.GONE);
//            }
        }


    }

    @Override
    public int getItemCount() {
        return data.size();
    }
}